package com.baeldung.keycloak;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClients;
import org.keycloak.authorization.client.AuthzClient;
import org.keycloak.authorization.client.Configuration;
import org.keycloak.representations.AccessTokenResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@Slf4j
public class UserRegisterController {
    private final static String authServerUrl = "http://localhost:8083/auth";
    private final static String realm = "SpringBootKeycloak";
    // private final static String clientId = "login-app";
    private final static String clientId = "account";
    private final static Map<String, Object> clientCredentials = Map.of("secret","2593357d-c297-4331-be37-52740f900601");
    private final static HttpClient httpClient = HttpClients.createDefault();

    @GetMapping("/auth")
    public void register(String userId) {
        Configuration configuration = new Configuration(authServerUrl, realm, clientId, clientCredentials, httpClient);

        // AuthorizationResource resource = AuthzClient.create(configuration).authorization(userId, "123456");
        AccessTokenResponse response = AuthzClient.create(configuration).obtainAccessToken(userId, "123456");

        System.out.println(response);
    }
}
